﻿using Client.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFCustomControlExample.Model;
using ZCustomControl.Models;
using Helpers;
using System.Windows.Input;

namespace WPFCustomControlExample.ViewModel
{
    public class SortButtonExampleViewModel : ViewModelBase
    {
        #region Fields

        private SortMode _numSortMode = SortMode.Des;
        
        #endregion

        #region Properties

        public ObservableCollection<PaginationTestData> Datas { get; set; }
        public SortMode NumSortMode
        {
            get => _numSortMode;
            set
            {
                _numSortMode = value;
                NumSortMethod(_numSortMode);
                RaisePropertyChanged("NumSortMode");
            }
        }

        #endregion

        #region Constructors

        public SortButtonExampleViewModel()
        {
            Datas = new ObservableCollection<PaginationTestData>();
            for(int i = 0; i < 20; i++)
            {
                Datas.Add(new PaginationTestData() { DataNumber = i, DataName = i + "个数据" });
            }
            NumSortMethod(NumSortMode);
        }
        
        #endregion

        #region Private Methods

        private void NumSortMethod(SortMode sortMode)
        {
            if(Datas == null)
            {
                return;
            }

            List<PaginationTestData> dataList = Datas.ToList();

            if(sortMode == SortMode.Asc)
            {
                dataList.Sort((x, y) =>
                {
                    if (x.DataNumber > y.DataNumber)
                    {
                        return 1;
                    }
                    else if (x.DataNumber == y.DataNumber)
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                });
            }
            else if(sortMode == SortMode.Des)
            {
                dataList.Sort((x, y) =>
                {
                    if(x.DataNumber < y.DataNumber)
                    {
                        return 1;
                    }
                    else if(x.DataNumber == y.DataNumber)
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                });
            }

            Datas.Clear();
            Datas.AddRange(dataList); 
        }

        #endregion

        #region Public Methods
        #endregion
    }
}
